﻿package mortal.component.GCatogeryList
{
    import com.gengine.global.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;

    public class GCatogeryList extends GSprite
    {
        private var _headContainer:GSprite;
        private var _pane:GScrollPane;
        private var _content:GSprite;
        private var _list:GTileList;
        private var _topPoint:GBitmap;
        private var _downPoint:GBitmap;
        private var _heads:Array;
        private var _selectedItem:ICatogeryListHead;
        private var _myWidth:int;
        private var _myHeight:int;
        private var _listItemHeight:int;
        private var _listHeight:int;
        private var _headGapOnExpand:int;
        private var _tweening:Boolean = false;
        private var _headGap:int = 1;
        private var _nullDataProvider:DataProvider;
        private var _currSelHead:ICatogeryListHead;
        private var _isNeedScroBar:Boolean = true;
        private var _expandTimerId:int = -1;
        private var _unexpandTimerId:int = -1;

        public function GCatogeryList(param1:int = 0, param2:int = 0)
        {
            this._nullDataProvider = new DataProvider();
            this._myWidth = param1;
            this._myHeight = param2;
            return;
        }// end function

        public function set isNeedScroBar(param1:Boolean) : void
        {
            this._isNeedScroBar = param1;
            if (this._isNeedScroBar)
            {
                this.addChild(this._pane);
                this._pane.source = this._content;
                this._pane.drawNow();
            }
            else
            {
                if (this._pane.parent)
                {
                    this._pane.parent.removeChild(this._pane);
                }
                this.addChild(this._content);
                this._content.y = 2;
            }
            return;
        }// end function

        public function get isNeedScroBar() : Boolean
        {
            return this._isNeedScroBar;
        }// end function

        public function get scrollPane() : GScrollPane
        {
            return this._pane;
        }// end function

        public function get headGap() : int
        {
            return this._headGap;
        }// end function

        public function set headGap(param1:int) : void
        {
            this._headGap = param1;
            return;
        }// end function

        public function createHeads(param1:Class, param2:Array, param3:int, param4:int) : void
        {
            var _loc_6:ICatogeryListHead = null;
            this._heads = [];
            var _loc_5:int = 0;
            while (_loc_5 < param2.length)
            {
                
                _loc_6 = UICompomentPool.getUICompoment(param1);
                _loc_6.index = _loc_5;
                _loc_6.setSize(param3, param4);
                _loc_6.updateData(param2[_loc_5]);
                _loc_6.y = (param4 + this._headGap) * _loc_5;
                this._headContainer.addChild(_loc_6 as DisplayObject);
                this._heads[_loc_5] = _loc_6;
                GSprite(_loc_6).configEventListener(MouseEvent.CLICK, this.clickHeadItemHandler);
                _loc_5++;
            }
            this._list.width = param3;
            this._list.columnWidth = param3;
            this.checkIsNeedScrollbar();
            if (this._pane && this._pane.parent)
            {
                this._pane.verticalScrollPosition = 0;
            }
            return;
        }// end function

        public function updateHeads(param1:Array) : void
        {
            var _loc_2:ICatogeryListHead = null;
            var _loc_3:int = 0;
            if (this._heads && this._heads.length)
            {
                _loc_3 = 0;
                while (_loc_3 < this._heads.length)
                {
                    
                    _loc_2 = this._heads[_loc_3] as ICatogeryListHead;
                    _loc_2.updateData(param1[_loc_3]);
                    _loc_3++;
                }
            }
            return;
        }// end function

        private function clickHeadItemHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as ICatogeryListHead;
            if (_loc_2.isExpanding)
            {
                this.unexpandItem(_loc_2.index);
            }
            else
            {
                this.expandItem(_loc_2.index);
            }
            if (this._currSelHead && this._currSelHead != _loc_2)
            {
                this._currSelHead.isSelected = false;
            }
            this._currSelHead = _loc_2;
            this._currSelHead.isSelected = true;
            return;
        }// end function

        public function setDataProvider(param1:int, param2:DataProvider, param3:Boolean = false) : void
        {
            var _loc_4:* = this._heads[param1];
            if (_loc_4 == null)
            {
                return;
            }
            _loc_4.dataProvider = param2;
            if (param3)
            {
                if (this._selectedItem != null && this._selectedItem.index == param1)
                {
                    this.expandItem(param1);
                }
            }
            return;
        }// end function

        public function setCellRender(param1:int, param2:Class, param3:Boolean = false) : void
        {
            var _loc_4:ICatogeryListHead = null;
            var _loc_5:int = 0;
            if (param3)
            {
                _loc_5 = 0;
                while (_loc_5 < this._heads.length)
                {
                    
                    _loc_4 = this._heads[_loc_5];
                    _loc_4.cellRender = param2;
                    _loc_5++;
                }
            }
            else
            {
                _loc_4 = this._heads[param1];
                if (_loc_4 == null)
                {
                    return;
                }
                _loc_4.cellRender = param2;
            }
            return;
        }// end function

        public function setCellHeight(param1:int, param2:int, param3:Boolean = false) : void
        {
            var _loc_4:ICatogeryListHead = null;
            var _loc_5:int = 0;
            if (param3)
            {
                _loc_5 = 0;
                while (_loc_5 < this._heads.length)
                {
                    
                    _loc_4 = this._heads[_loc_5];
                    _loc_4.cellHeight = param2;
                    _loc_5++;
                }
            }
            else
            {
                _loc_4 = this._heads[param1];
                if (_loc_4 == null)
                {
                    return;
                }
                _loc_4.cellHeight = param2;
            }
            return;
        }// end function

        public function setListItemHeight(param1:int) : void
        {
            this._listItemHeight = param1;
            return;
        }// end function

        public function expandItem(param1:int, param2:int = 0) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_6:ICatogeryListHead = null;
            var _loc_7:int = 0;
            if (param1 >= this._heads.length || this._tweening || this._expandTimerId > 0 || this._unexpandTimerId > 0)
            {
                this._tweening = false;
                return;
            }
            if (this._selectedItem != null)
            {
                this._selectedItem.unexpand();
            }
            this._selectedItem = this._heads[param1] as ICatogeryListHead;
            this._selectedItem.expand();
            this._list.setStyle("cellRenderer", this._selectedItem.cellRender);
            this._list.dataProvider = this._selectedItem.dataProvider;
            if (this._selectedItem.dataProvider != null && this._selectedItem.dataProvider.length > 0)
            {
                if (this._list.dataProvider.length > param2)
                {
                    this._list.selectedIndex = param2;
                }
                else
                {
                    this._list.selectedIndex = -1;
                }
            }
            else
            {
                this._list.selectedIndex = -1;
            }
            if (this._headGapOnExpand > 0)
            {
                _loc_3 = (this._selectedItem.cellHeight + this._headGapOnExpand) * (param1 + 1);
            }
            else
            {
                _loc_3 = (this._selectedItem.cellHeight + this._headGap) * (param1 + 1);
            }
            _loc_4 = this._selectedItem.dataProvider.length * this._listItemHeight;
            var _loc_5:* = this._selectedItem.cellHeight * (this._heads.length - param1 - 1);
            this._list.rowHeight = this._listItemHeight == 0 ? (this._selectedItem.cellHeight) : (this._listItemHeight);
            this._list.y = _loc_3;
            this._list.height = this._listHeight == 0 ? (_loc_4) : (this._listHeight);
            this._topPoint.y = 0;
            this._content.addChild(this._topPoint);
            this._downPoint.y = this._list.y + this._list.height + _loc_5;
            this._list.drawNow();
            if (_loc_4 > this._list.height)
            {
                _loc_4 = this._list.height;
            }
            var _loc_8:int = 0;
            while (_loc_8 <= param1)
            {
                
                _loc_6 = this._heads[_loc_8] as ICatogeryListHead;
                if (this._selectedItem.dataProvider.length > 0 && this._headGapOnExpand > 0)
                {
                    _loc_7 = _loc_8 * (_loc_6.cellHeight + this._headGapOnExpand);
                }
                else
                {
                    _loc_7 = _loc_8 * (_loc_6.cellHeight + this._headGap);
                }
                if (Math.abs(_loc_6.y - _loc_7) < 2)
                {
                }
                else
                {
                    TweenMax.to(_loc_6, 0.2, {y:_loc_7, onComplete:this.expandCompleted});
                    this._tweening = true;
                }
                _loc_8++;
            }
            while (_loc_8 < this._heads.length)
            {
                
                _loc_6 = this._heads[_loc_8];
                _loc_7 = _loc_4 + _loc_8 * (_loc_6.cellHeight + this._headGap);
                if (Math.abs(_loc_6.y - _loc_7) < 2)
                {
                }
                else
                {
                    TweenMax.to(_loc_6, 0.2, {y:_loc_7, onComplete:this.expandCompleted});
                    this._tweening = true;
                }
                _loc_8++;
            }
            var _loc_9:int = 250;
            Global.instance.callLater(this.expandCompleted);
            return;
        }// end function

        private function expandCompleted() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._expandTimerId = -1;
            this._tweening = false;
            this._content.addChild(this._list);
            this.checkIsNeedScrollbar();
            return;
        }// end function

        private function checkIsNeedScrollbar() : void
        {
            this.isNeedScroBar = this._content.height > this._myHeight + 20;
            return;
        }// end function

        public function unexpandItem(param1:int) : void
        {
            var _loc_5:ICatogeryListHead = null;
            var _loc_6:int = 0;
            if (param1 >= this._heads.length || this._tweening || this._unexpandTimerId > 0 || this._expandTimerId > 0)
            {
                return;
            }
            var _loc_2:* = this._heads[param1];
            _loc_2.unexpand();
            this._list.dataProvider = this._nullDataProvider;
            this._list.drawNow();
            this._list.y = 0;
            this._topPoint.y = 0;
            this._downPoint.y = 0;
            this._tweening = true;
            var _loc_3:* = param1 == (this._heads.length - 1) ? (-1) : (param1);
            var _loc_4:* = _loc_3 + 1;
            while (_loc_4 < this._heads.length)
            {
                
                _loc_5 = this._heads[_loc_4] as ICatogeryListHead;
                _loc_6 = _loc_4 * (_loc_5.cellHeight + this._headGap);
                TweenMax.to(_loc_5, 0.2, {y:_loc_6, onComplete:this.unExpandCompleted});
                _loc_4++;
            }
            Global.instance.callLater(this.unExpandCompleted);
            this._selectedItem = null;
            return;
        }// end function

        private function unExpandCompleted() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._unexpandTimerId = -1;
            this._tweening = false;
            if (this._list.parent)
            {
                this._list.parent.removeChild(this._list);
            }
            this.checkIsNeedScrollbar();
            return;
        }// end function

        public function unexpandAllItem() : void
        {
            if (this._selectedItem != null)
            {
                this.unexpandItem(this._selectedItem.index);
            }
            this._list.selectedIndex = -1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseEnabled = true;
            this.mouseChildren = true;
            this._pane = UIFactory.gScrollPanel(0, 2, this._myWidth, this._myHeight - 4, this);
            this._content = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this.addChild(this._content);
            this._topPoint = UIFactory.gBitmap(ImagesConst.SplitLine, 0, 0, this._content);
            this._topPoint.visible = false;
            this._list = UIFactory.tileList(0, 0, this._myWidth, this._myHeight, null);
            this._list.columnWidth = this._myWidth;
            this._downPoint = UIFactory.gBitmap(ImagesConst.SplitLine, 0, 0, this._content);
            this._downPoint.visible = false;
            this._headContainer = UICompomentPool.getUICompoment(GSprite) as GSprite;
            this._content.addChild(this._headContainer);
            return;
        }// end function

        public function get tileList() : GTileList
        {
            return this._list;
        }// end function

        public function setListSize(param1:Number, param2:Number) : void
        {
            this._myWidth = param1;
            this._myHeight = param2;
            this._pane.setSize(param1, param2);
            this._list.setSize(param1, param2);
            this._list.columnWidth = param1;
            return;
        }// end function

        public function setItemListSize(param1:int, param2:int, param3:int, param4:int) : void
        {
            this._list.setSize(param1, param2);
            this._list.columnWidth = param3;
            this._list.rowHeight = param4;
            this._listHeight = param2;
            this._listItemHeight = param4;
            return;
        }// end function

        public function set verticalScrollPolicy(param1:String) : void
        {
            this._list.verticalScrollPolicy = param1;
            return;
        }// end function

        public function set direction(param1:String) : void
        {
            this._list.direction = param1;
            return;
        }// end function

        public function set headGapOnExpand(param1:int) : void
        {
            this._headGapOnExpand = param1;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._expandTimerId > 0)
            {
                clearTimeout(this._expandTimerId);
                this._expandTimerId = -1;
            }
            if (this._unexpandTimerId > 0)
            {
                clearTimeout(this._unexpandTimerId);
                this._unexpandTimerId = -1;
            }
            DisplayUtil.delNotUse(this._heads, 0);
            this._heads = null;
            this._headContainer.dispose(param1);
            this._headContainer = null;
            this._pane.dispose(param1);
            this._pane = null;
            this._content.dispose(param1);
            this._content = null;
            this._list.dispose(param1);
            this._list = null;
            this._selectedItem = null;
            this._tweening = false;
            this._topPoint.dispose(param1);
            this._topPoint = null;
            this._downPoint.dispose(param1);
            this._downPoint = null;
            this._listItemHeight = 0;
            this._listHeight = 0;
            this._currSelHead = null;
            this._headGapOnExpand = 0;
            this._isNeedScroBar = true;
            super.disposeImpl(param1);
            return;
        }// end function

    }
}
